import {
  $wuxToast
} from '../../es/index'
Page({
  data: {
    "token": "",
    "userIcon": "",
    "options": [{
      icon: "/icons/person-outline.svg",
      title: "个人信息"
    }, {
      icon: "/icons/cloud-download-outline.svg",
      title: "文件下载记录"
    }, {
      icon: "/icons/cloud-upload-outline.svg",
      title: "文件上传记录"
    }, {
      icon: "/icons/mail-outline.svg",
      title: "消息记录"
    }, {
      icon: "/icons/star-outline.svg",
      title: "收藏记录"
    }]
  },

  //每次需要用户信息的界面都需要判断有没有token
  onShow: function() {
    var that = this;
    //过期或者没有登录都会返回一个null，否则返回一个字符串，说明已经登录
    var token = this.getToken();
    console.log(token);
    if (token != null) {
      that.setData({
        token: token
      })
    } else {
      that.setData({
        token: ""
      })
    }
  },
  //登录
  login: function() {
    console.log("logining");
    var that = this;
    var identity = this.data.identity;
    var password = this.data.password;
    wx.showLoading({
      title: '登陆中',
    })
    wx.request({
      //后期改成服务器路径
      // url: 'http://localhost/user/login',
      url:'https://szsyph.cn/user/login',
      method: "POST",
      data: {
        identity: identity, //该属性值可以是用户名或邮箱
        password: password
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
      },
      fail:function(res){
        wx.hideLoading({});
      },
      success:function(res) {
        wx.hideLoading({
          complete: (res) => {},
        });
        var response = res.data;
        //如果返回码为200说明登陆成功
        if (response.code == 200) {
          console.log(response.data.token);
          that.saveToken(response.data.token, 3 * 60 * 60);
          //保存用户头像路径
          that.setData({
            //后期改成服务器路径
            // userIcon: "http://localhost/" + response.data.userIcon,
            userIcon: "https://szsyph.cn/" + response.data.userIcon,
            token: response.data.token
          });
        } else {
          console.log(response.message);
          that.showToastCancel(response.message);
        }
      }
    });
  },
  //登出
  logout: function() {
    wx.showModal({
      title: '真的要退出吗?',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync("token");
          wx.removeStorageSync("tokenTime");
          this.setData({
            token: ""
          });
        }
      },
    })

  },
  saveToken: function(value, time) {
    wx.setStorageSync("token", value);
    var seconds = parseInt(time);
    var newtime = Date.parse(new Date());
    newtime = newtime / 1000 + seconds;
    wx.setStorageSync("tokenTime", newtime + "");
  },
  getToken: function() {
    var deadtime = parseInt(wx.getStorageSync("tokenTime"));
    if (deadtime) {
      if (parseInt(deadtime) < Date.parse(new Date()) / 1000) {
        console.log("过期了");
        return null;
      }
    }
    //如果没有时间限制 或者 有时间限制但是没有过期
    var res = wx.getStorageSync("token");
    if (res) {
      return res
    } else {
      return null
    }
  },
  //获取输入框的值
  identityOnInput: function(e) {
    console.log(e);
    this.setData({
      identity: e.detail.value
    });
  },
  passwordOnInput: function(e) {
    console.log(e);
    this.setData({
      password: e.detail.value
    });
  },
  //弹出提示框
  showToastCancel(message) {
    $wuxToast().show({
      type: 'cancel',
      duration: 1500,
      color: '#fff',
      text: message,
    })
  },
  navigateToDetail: function(e) {
    var token = this.data.token;
    if (e.currentTarget.dataset.index == 0) {
      wx.navigateTo({
        url: 'info/info?token='+token,
      })
    } else if (e.currentTarget.dataset.index == 1) {
      wx.navigateTo({
        url: 'download/download?token=' + token,
      })
    } else if (e.currentTarget.dataset.index == 2) {
      wx.navigateTo({
        url: 'upload/upload?token=' + token,
      })
    }
  }
})